﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BackupAssurance.Library.Enums;
using System.IO;

namespace BackupAssurance.impVerifyFileExists
{
    public class filestructure
    {
        public string _Maskpath;
        public string _Maskextension;
        public string _Maskfilename;
        public bool wasfilefound;
        public string _OriginalPattern;

        public filestructure()
        {

        }

        public int init(string input)
        {

            _OriginalPattern = input;
            string[] afiles = input.Split('|');
            
            _Maskpath = afiles[0];

            if (afiles.Length >= 2)
            {

                _Maskfilename = afiles[1];
            }

            if (afiles.Length >=3 )
            {
                _Maskextension = afiles[2];
                if (_Maskextension.Substring(0, 1) == ".")
                {
                    _Maskextension = _Maskextension.Substring(1);
                }
            }           

            return afiles.Length;
        }

        public TypeFileMask getfoldermask()
        {
            return determinemask(_Maskpath);
        }

        public TypeFileMask get_Maskfilenamemask()
        {
            return determinemask(_Maskfilename);
        }

        public TypeFileMask get_Maskextensionmask()
        {
            return TypeFileMask.none;
        }


        public bool FileMatchesPattern(string processfolder, FileInfo file)
        {
            Boolean b_Maskfilenamematch = false;
            Boolean b_Maskpathmatch = false;
            Boolean b_Maskextensionmatch = false;
            int processfolderlength = processfolder.Length;
            string sExtension = file.Extension; 
            sExtension = sExtension.ToUpper();

            try
            {
                if ((sExtension != "") && sExtension.Substring(0, 1) == ".")
                {
                    sExtension = sExtension.Substring(1);
                }

                if ((sExtension == _Maskextension) || (_Maskextension == "") || (_Maskextension == "*"))
                {
                    b_Maskextensionmatch = true;
                }

                //trim the extension from the filename for comparison
                string sfilename = file.Name;
                if (sfilename != "" && sExtension != "")
                {
                    sfilename = sfilename.Replace(file.Extension, "");
                }

                sfilename = sfilename.ToUpper();

                if ((sfilename == _Maskfilename) || (_Maskfilename == "") || (_Maskfilename == "*"))
                {
                    b_Maskfilenamematch = true;
                }
                else
                {
                    b_Maskfilenamematch = filetomaskCompare(sfilename, _Maskfilename);
                }


                string sdirectoryname = file.DirectoryName.Substring(processfolderlength);
                sdirectoryname = sdirectoryname.ToUpper();
                if ((sdirectoryname == _Maskpath) || (_Maskpath == "") || (_Maskpath == "*"))
                {
                    b_Maskpathmatch = true;
                }
                else
                {
                    b_Maskpathmatch = filetomaskCompare(sdirectoryname, _Maskpath);
                }

            }
            catch (Exception)            {
                
                return false;
            }


            if (b_Maskpathmatch && b_Maskfilenamematch && b_Maskextensionmatch)
            {
                return true;
             } else {
                 return false;
             }

        }

        private Boolean filetomaskCompare(string strInFileInfo, string filterfromxml)
        {

            TypeFileMask enumMask;
            String strNewfromXML;
            int ilength = 0;
            int iInputLength = strInFileInfo.Length;
            strInFileInfo = strInFileInfo.ToUpper();           
            
                enumMask = determinemask(filterfromxml);

                strNewfromXML = filterfromxml.Replace("*", "");
                ilength = strNewfromXML.Length;

                switch (enumMask)
                {
                    case TypeFileMask.none:
                        if (strInFileInfo == strNewfromXML) { return true; }
                        break;
                    case TypeFileMask.left:
                        if (ilength <= iInputLength)
                        {
                            if (strInFileInfo.Substring(iInputLength - ilength, ilength) == strNewfromXML)
                            {
                                return true;
                            }
                        }
                        break;
                    case TypeFileMask.right:
                        if (ilength <= iInputLength)
                        {
                            if (strInFileInfo.Substring(0, ilength) == strNewfromXML)
                            {
                                return true;
                            }
                        }
                        break;
                    case TypeFileMask.both:
                        if (strInFileInfo.IndexOf(strNewfromXML) >= 0)
                        {
                            return true;
                        }
                        break;
                    default:
                        return false;
                }

            
            return false;
        }


        private TypeFileMask determinemask(string strInput)
        {


            Boolean leftmostison = false;
            Boolean rightmostison = false;
            int lenght = strInput.Length;

            if (strInput.Substring(0, 1) == "*") { leftmostison = true; }
            if (strInput.Substring(lenght - 1, 1) == "*") { rightmostison = true; }

            if ((leftmostison == false) && (rightmostison == false))
            {
                return TypeFileMask.none;
            }
            else if ((leftmostison == true) && (rightmostison == true))
            {
                return TypeFileMask.both;
            }
            else if ((leftmostison == true) && (rightmostison == false))
            {
                return TypeFileMask.left;
            }
            else if ((leftmostison == false) && (rightmostison == true))
            {
                return TypeFileMask.right;
            }
            else
            {
                //should never happen
                return TypeFileMask.none;
            }
        }


    }

    
}
